package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.annotation.AdminLogAnnotation;
import com.market.bean.*;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;


import com.market.mapper.*;

import com.market.utils.Constent;
import com.market.utils.StringUtil;

import com.market.bean.vo.admin.AdminOrderDetailVO;
import com.market.service.admin.AdminOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: OrderServiceImpl
 * @description: TODO :
 * @author: March
 * @version: 1.0
 * @datetime: 2022/12/28 21:48
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketChannelMapper marketChannelMapper;


    /**
     * @param info
     * @param start
     * @param end
     * @return com.market.bean.common.CommonData<com.market.bean.MarketOrder>
     * @description:
     * @author : Zqp
     * @version :
     * @date : 2022/12/28 23:17
     */
    @Override
    public CommonData<MarketOrder> list(BasePageInfo info, Date[] timeArray, Date start, Date end, Short[] orderStatusArray, Integer userId, String orderSn) {

        MarketOrderExample example = new MarketOrderExample();

        if (info != null && info.getLimit() != 0) {
            PageHelper.startPage(info.getPage(), info.getLimit());
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        } else {
            PageHelper.startPage(0, 0, true, true, true);
        }


        //创建返回对象
        List<MarketOrder> list = null;
        PageInfo<MarketOrder> pageInfo = null;

        //如果orderId不为空,则直接通过orderId进行查询
//        if (orderId != null) {
//            MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
//            //如果查询结果为空,则返回提示信息
//            if (marketOrder == null) {
//                return null;
//            }
//            list.add(marketOrder);
//            pageInfo = new PageInfo<>(list);
//            return CommonData.data(pageInfo);
//        }

        //如果orderId为空,则根据其他条件查询


        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

//        if (start != null) {
//            criteria.andAddTimeGreaterThanOrEqualTo(start);
//        }
//        if (end != null) {
//            criteria.andEndTimeLessThanOrEqualTo(end);
//        }

        //根据时间查询订单时,指定为按创建时间查询
        if (start != null || end != null) {
            criteria.andAddTimeBetween(start, end);
        }

        if (orderStatusArray != null) {
            //将接收到的订单状态数组转为List
            List<Short> orderStatusList = Arrays.asList(orderStatusArray);
            criteria.andOrderStatusIn(orderStatusList);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }

        list = marketOrderMapper.selectByExample(example);
        pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @AdminLogAnnotation
    @Override
    public int refund(Map map) {
        Integer orderId = (Integer) map.get("orderId");
        BigDecimal refundMoney = (BigDecimal) map.get("refundMoney");

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setRefundAmount(refundMoney); //退款金额
        marketOrder.setAftersaleStatus(Constent.AFTERSALE_STATUS_3);//售后状态
        marketOrder.setRefundTime(new Date()); //退款时间
        marketOrder.setOrderStatus(Constent.REFUNDED);//订单状态

//        marketOrder.setRefundType();   退款方式
//        marketOrder.setRefundContent();  退款备注

        int code = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

        return code;
    }

    @Override
    public int reply(Map map) {
        //{commentId: 764, content: "666"}
        MarketComment marketComment = new MarketComment();
        marketComment.setContent((String) map.get("content"));
        marketComment.setId((Integer) map.get("commentId"));

        marketComment.setUpdateTime(new Date());

        int code = marketCommentMapper.updateByPrimaryKeySelective(marketComment);
        return code;
    }

    @Override
    public AdminOrderDetailVO getDetail(Integer id) {
        //获取订单信息
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        if (marketOrder == null) {
            return null;
        }

        //获取订单商品信息
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example);

        //获取用户信息
        Integer userId = marketOrder.getUserId();
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);

        //封装VO
        AdminOrderDetailVO adminOrderDetailVO = new AdminOrderDetailVO();
        adminOrderDetailVO.setOrder(marketOrder);
        adminOrderDetailVO.setOrderGoods(marketOrderGoods);
        adminOrderDetailVO.setUser(marketUser);

        return adminOrderDetailVO;
    }

    @AdminLogAnnotation
    @Override
    public int shipOrder(Map map) {

        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");

        //封装订单信息
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setShipChannel(shipChannel);
        marketOrder.setShipSn(shipSn);

        marketOrder.setOrderStatus(Constent.SHIPPED);//更新订单状态

        Date date = new Date();
        marketOrder.setShipTime(date);//更新发货时间
        marketOrder.setUpdateTime(date);//更新订单更新时间

        //更新订单信息
        int code = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return code;
    }

    @AdminLogAnnotation
    @Override
    public int deleteOrder(Map map) {
        Integer orderId = (Integer) map.get("orderId");

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setDeleted(true);
        int code = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return code;
    }

    @Override
    public List<MarketChannel> channel() {
        List<MarketChannel> list = marketChannelMapper.selectByExample(null);
        return list;
    }

}
